package com.system.xingzhi.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.UserMapper;
import com.system.xingzhi.model.ReturnData;
import com.system.xingzhi.model.User;
import com.system.xingzhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.system.xingzhi.util.JwtUtil;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String userId) {
        ReturnData returnData=new ReturnData();
        try {
            userMapper.deleteByPrimaryKey(userId);
            returnData.setCode(0);
            returnData.setDetail("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("删除失败");
            return  returnData;
        }

        return returnData;
    }

    @Override
    public ReturnData insert(User record) {
        ReturnData returnData=new ReturnData();
        try {
            userMapper.insert(record);
            returnData.setCode(0);
            returnData.setDetail("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("添加失败");
            return  returnData;
        }
        return returnData;
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(User record) {
        ReturnData returnData=new ReturnData();
        try {
            if(record.getUserRoleId()==""){
                record.setUserRoleId(null);
            }
            userMapper.updateByPrimaryKey(record);
            returnData.setCode(0);
            returnData.setDetail("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("修改失败");
            return  returnData;
        }
        return returnData;
    }

    /**
     * 通过传入参数，分页查询用户
     * @param map
     * @return
     */
    @Override
    public ReturnData selectByParameter(String keyWord,Integer pageNum, Integer pageSize) {
        ReturnData returnData=new ReturnData();
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> queryResult = userMapper.selectByParameter(keyWord);

        returnData.setData(new PageInfo<>(queryResult));
        returnData.setCode(0);
        returnData.setDetail("查询用户数据成功");
        return returnData;
    }

    /**
     * 登录验证账号和密码，并返回token值
     * @param map
     * @return
     */
    @Override
    public ReturnData login( Map<String,String> map) {
        User user = userMapper.selectByNameAndPassword(map);
        ReturnData returnData=new ReturnData();
        if(user != null){
            returnData.setData(user);
            if (user.getIsForbid()==0){
                JwtUtil jwtUtil=new JwtUtil();
                returnData.setNewToken(jwtUtil.creatToken(user));
                returnData.setCode(0);
                returnData.setDetail("登陆成功");
            }else{
                returnData.setCode(1);
                returnData.setDetail("登陆失败，账号已被禁用");
            }
        }else{
            returnData.setCode(1);
            returnData.setDetail("登陆失败，请重新输入账号和密码");
        }
        return returnData;
    }
}
